package com.second.service.impl;

import com.second.mapper.FGoodsMapper;
import com.second.mapper.FOrdSeckillMapper;
import com.second.model.dto.classify.SeckillGoodsDto;
import com.second.model.pojo.FGoods;
import com.second.model.pojo.FOrdSeckill;
import com.second.service.IFOrdSeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class FOrdSeckillServiceImpl implements IFOrdSeckillService {
    @Autowired
    private FOrdSeckillMapper ordSeckillMapper;

    @Autowired
    private FGoodsMapper goodsMapper;

    @Override
    public String isEnd(Integer secId) {
        Optional<FOrdSeckill> seckill = ordSeckillMapper.findById(secId);
        Date timeNow = new Date();
        //时间
        if (timeNow.getTime() - seckill.get().getStartTime().getTime() > 0) {

            return "200";
        } else
            return "400";
    }

    @Override
    public List<SeckillGoodsDto> findAllSeckillGoods() {
        List<FOrdSeckill> seckillList = ordSeckillMapper.findAllByStatus(1);
        List<SeckillGoodsDto> list = new ArrayList<>();
        if (null!=seckillList){
            for (FOrdSeckill seckill : seckillList) {
                SeckillGoodsDto goodsDto = new SeckillGoodsDto();
                goodsDto.setSeckillId(seckill.getId());
                goodsDto.setEndTime(seckill.getEndTime());
                goodsDto.setNum(seckill.getNum());
                goodsDto.setPrice(seckill.getPrice());
                FGoods goods = goodsMapper.getById(seckill.getGoodsId());
                goodsDto.setGoodsCurrentPrice(goods.getGoodsCurrentPrice());
                goodsDto.setGoodsName(goods.getGoodsName());
                goodsDto.setGoodsPicture(goods.getGoodsPicture());
                list.add(goodsDto);
            }
            return list;
        }else return null;

    }

    @Override
    public void updateStatus(Integer secId) {
        FOrdSeckill seckill = ordSeckillMapper.getById(secId);
        seckill.setStatus(0);
        ordSeckillMapper.save(seckill);
    }
}
